// pages/password/password.js
const app = getApp()
const util = require('../../utils/util.js')
const CryptoJS = require('../../utils/aes-jia.js');
var timestamp = Date.parse(new Date()) / 1000; 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code:'',
    phoneNumber: '',
    phoneq3:'',
    phoneh4:'',
    codeTime:'',
    codeStatus: false,
    payPwdNew:'',
    xg:'',
    pswdinput:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if(options.xg==1){
      that.setData({
        xg:1
      })
    }
  },
 
  codeInput(e) {
    this.setData({
      code: e.detail.value
    })
  },
  pswdinput(e){
    var pswdinput = e.detail.value
    var password = timestamp + '|' + pswdinput
    // console.log('加密', pswdinput, password)
    this.setData({
      pswdinput: pswdinput,
      payPwdNew: CryptoJS.EncryptBASE64(password)
    })
    console.log('加密1111', this.data.payPwdNew, password)
  },
  sendCode() {
    var phone = this.data.phoneNumber.trim();
    var that = this;
    app.req({
      url: "blessmini/user/sendCode?telphone=" + phone + '&type='+ 2,
      data: {},
      method: "post",
      success: function (res) {
        console.log('发送验证码',res)
        if (200 == res.code) {
          wx.showToast({
            title: '验证码发送成功，请注意查收',
            icon: 'none',
            duration: 2000
          })
          that.setData({
            codeTime: 60,
            codeStatus: true
          })
          var currentTime = that.data.codeTime
          var interval = setInterval(function () {
            currentTime--;
            that.setData({
              codeTime: currentTime
            })
            if (currentTime == 0) {
              clearInterval(interval)
              that.setData({
                codeTime: 0,
                codeStatus: false
              })
            }
          }, 1000)
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  save: function () {
    var that = this
    var modifyType = ''
    if(that.data.code==''){
      wx.showToast({
        title: '请输入验证码',
        icon: 'none',
        duration: 2000
      })
      return;
    }else
    if (that.data.payPwdNew==''){
      wx.showToast({
        title: '请输入支付密码',
        icon: 'none',
        duration: 2000
      })
      return;
    }else
      if (that.data.pswdinput.length < 6) {
      wx.showToast({
        title: '请输入6位支付密码',
        icon: 'none',
        duration: 2000
      })
      return;
    }else{
    if(that.data.xg==1){
      modifyType=3
    }else{
      modifyType=1
    }
      app.req({
        url: "blessmini/user/modifyPayPwd",
        data: {
          modifyType: modifyType,
          code:that.data.code,
          payPwdNew: that.data.payPwdNew
        },
        method: "post",
        success: function (res) {
          if (res.code==200) {
            console.log('操作成功',res)
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 1000)
            wx.showToast({
              title: '设置成功',
              icon: 'none',
              duration: 2000
            })
           
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var phone = wx.getStorageSync('phone')
    this.setData({
      phoneNumber: phone,
      phoneq3: phone.substr(0,3),
      phoneh4: phone.substr(7, 4)
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})